/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

public class Text extends InputPageElement {
	private static final String	TEXT_TYPE		= "text";
	private static final String	READONLY_KEY	= "readonly";
	private static final String	MAXLENGTH_KEY	= "maxlength";

	{
		this.setType(TEXT_TYPE);
	}

	public Text() {
		super();
	}

	public Text(final String _name, final String _value) {
		super(_name, _value);
	}

	public Text(final String _name, final Object _value) {
		super(_name, _value != null ? _value.toString() : "");
	}

	public Text(final String _value) {
		super(_value);
	}

	public Integer getMaxLength() {
		final String maxlength = this.getAttributeValue(MAXLENGTH_KEY);
		if (maxlength == null) {
			return null;
		} else {
			return Integer.valueOf(maxlength);
		}
	}

	public Text setMaxLength(final Integer _value) {
		if (_value == null || _value < 0) { return this; }

		return (Text) this.setAttribute(MAXLENGTH_KEY, _value.toString());
	}

	public Text setReadOnly(final boolean _readOnly) {
		if (_readOnly) {
			this.setAttribute(READONLY_KEY, Boolean.toString(_readOnly));
		} else {
			this.removeAttribute(READONLY_KEY);
		}
		return this;
	}

	public boolean isReadOnly() {
		return Boolean.valueOf(this.getAttributeValue(READONLY_KEY));
	}
}
